package com.pet.serve.service;

import com.pet.serve.entity.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class ExportService {

    // 通用方法: 创建Excel表头
    private void createHeader(Sheet sheet, String[] headers) {
        Row header = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            header.createCell(i).setCellValue(headers[i]);
        }
    }

    // 通用方法: 将Excel转换为字节数组
    private byte[] workbookToBytes(Workbook workbook) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        workbook.write(out);
        workbook.close();
        return out.toByteArray();
    }

    // 导出 Customers
    public byte[] exportCustomers(List<Customers> customers) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Customers");

        String[] headers = {"用户ID", "用户账号", "真名", "级别", "权限"};
        createHeader(sheet, headers);

        int rowNum = 1;
        for (Customers customer : customers) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(customer.getId());
            row.createCell(1).setCellValue(customer.getUsername());
            row.createCell(2).setCellValue(customer.getName());
            row.createCell(3).setCellValue(customer.getLevel().toString());
            row.createCell(4).setCellValue(customer.getPermission().toString());
        }

        return workbookToBytes(workbook);
    }

    // 导出 Goods
    public byte[] exportGoods(List<Goods> goods) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Goods");

        String[] headers = {"Good ID", "Name", "Price", "Type", "Num", "Status"};
        createHeader(sheet, headers);

        int rowNum = 1;
        for (Goods good : goods) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(good.getGoodid());
            row.createCell(1).setCellValue(good.getName());
            row.createCell(2).setCellValue(good.getPrice().doubleValue());
            row.createCell(3).setCellValue(good.getType());
            row.createCell(4).setCellValue(good.getNum());
            row.createCell(5).setCellValue(good.getStatus());
        }

        return workbookToBytes(workbook);
    }

    // 导出 Pets
    public byte[] exportPets(List<Pets> pets) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Pets");

        // 创建表头
        String[] headers = {"Pet ID", "Name", "Type", "Variety", "Price", "Status"};
        createHeader(sheet, headers);

        // 填充数据
        int rowNum = 1;
        for (Pets pet : pets) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(pet.getPetid());
            row.createCell(1).setCellValue(pet.getName());
            row.createCell(2).setCellValue(pet.getType().name());  // 使用枚举的 name() 方法
            row.createCell(3).setCellValue(pet.getVariety());
            row.createCell(4).setCellValue(pet.getPrice().doubleValue());
            row.createCell(5).setCellValue(pet.getStatus().name());  // 使用枚举的 name() 方法
        }

        return workbookToBytes(workbook);
    }


    // 导出 Invest
    public byte[] exportInvests(List<Invest> invests) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Invests");

        String[] headers = {"Invest ID", "Username", "Money","Approval","Status", "Time"};
        createHeader(sheet, headers);

        int rowNum = 1;
        for (Invest invest : invests) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(invest.getInvestid());
            row.createCell(1).setCellValue(invest.getUsername());
            row.createCell(2).setCellValue(invest.getMoney().doubleValue());
            row.createCell(3).setCellValue(invest.getApproval());
            row.createCell(4).setCellValue(invest.getStatus().name());// 使用枚举的 name() 方法
            row.createCell(5).setCellValue(invest.getTime().toString());
        }

        return workbookToBytes(workbook);
    }
}
